﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RMS.Utils.MVP;
using RMS.Web.Shell.View;
using RMS.DataAccess.Entities;
using RMS.DataAccess.Repositories;
using RMS.Utils.Dicom;
using System.IO;
using RMS.DataAccess.Common;
using RMS.Utils.Authentication;

namespace RMS.Web.Shell.Presenter
{
    public class ManageRadiologyImagePresenter : Presenter<IManageRadiologyImageView>
    {
        RadiologyImageRepository _radiologyImageRepository;
        TechnicianRadiologyRequestsDtoRepository _technicianRadiologyRequestsDtoRepository;
        RadiologyRequestRepository _radiologyRequestRepository;
        IAuthentication _authentication;

        public ManageRadiologyImagePresenter(RadiologyImageRepository radiologyImageRepository,
         TechnicianRadiologyRequestsDtoRepository technicianRadiologyRequestsDtoRepository,
          RadiologyRequestRepository radiologyRequestRepository, IAuthentication authentication)
        {
            _radiologyImageRepository = radiologyImageRepository;
            _technicianRadiologyRequestsDtoRepository = technicianRadiologyRequestsDtoRepository;
            _radiologyRequestRepository = radiologyRequestRepository;
            _authentication = authentication;
        }


        private bool CheckImgExist(string path)
        {
            return File.Exists(path + ".jpg");
        }
        private bool CheckDataExist(string path)
        {
            return File.Exists(path + ".Xml");
        }

        public TechnicianRadiologyRequestsDto CurrentRequest(int RequestID)
        {
            return _technicianRadiologyRequestsDtoRepository.Find(item => {
                item.TechnicianID = _authentication.CurrentLogedInMember.Id;
                item.RequestId = RequestID;
            }).Single();
        }
        public EntityResult<TechnicianRadiologyRequestsDto> GetRequest(int patientId)
        {
            return _technicianRadiologyRequestsDtoRepository.Find(item => item.PatientID = patientId);
        }
        public EntityResult<TechnicianRadiologyRequestsDto> GetRequest(String patientName)
        {
            string[] names = patientName.Split(' ');
            return _technicianRadiologyRequestsDtoRepository.Find(items =>
                {
                    items.TechnicianID = _authentication.CurrentLogedInMember.Id;
                    items.FirstName = names[0];
                    if (names.Count() > 1)
                        items.FamilyName = names[1];
                });
        }


        public int insertRadiologyImage(RadiologyImage radiologyImage)
        {
            int id = _radiologyImageRepository.Insert(radiologyImage);
            updateRequestStatues(radiologyImage.RequestId);
            return id;
        }

        private void updateRequestStatues(int RequestID)
        {
            RadiologyRequest radiologyRequest = _radiologyRequestRepository.Find(item => item.RequestId = RequestID).Single();
            radiologyRequest.RequestStatues = true;
            _radiologyRequestRepository.Update(radiologyRequest);
        }


    }
}
